package com.zhbr.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.zhbr.entity.MPageInfo;
import com.zhbr.entity.TGdfwPqgd;
import com.zhbr.mvp.contract.HomeContract;
import com.zhbr.mvp.presenter.HomePresenter;
import com.zhbr.R;
import com.zhbr.entity.TabEntity;
import com.zhbr.mvp.ui.fragment.HomeFragment;
import com.zhbr.mvp.ui.fragment.MyFragment;
import com.zhbr.mvp.ui.fragment.OrderFragment;
import com.zhbr.mvp.base.view.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;

public class HomeActivity extends BaseActivity<HomePresenter> implements HomeContract.View {

    private Context mContext = this;

    private long lastTimeMillis=0;

    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;


    private String[] mTitles = {"首页", "工单", "我的"};
    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
            R.mipmap.tab_contact_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
            R.mipmap.tab_contact_select};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private List<TGdfwPqgd> list= new ArrayList<>();

    Random mRandom = new Random();


    @Override
    protected HomePresenter createPresenter() {
        return new HomePresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        mFragments.add(HomeFragment.newInstance());
        mFragments.add(OrderFragment.newInstance());
        mFragments.add(MyFragment.newInstance());

    }

    @Override
    protected void initData() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }
            @Override
            public void onTabReselect(int position) {

            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setOffscreenPageLimit(mTitles.length);
        viewPager.setCurrentItem(0);
        //获取工单数量
        mPresenter.getPqgd(0,5);
        TGdfwPqgd tGdfwPqgd = new TGdfwPqgd();
        tGdfwPqgd.setBxr("孟祥龙");
        tGdfwPqgd.setBxnr("Hello world!");
        tGdfwPqgd.setObjId("1231231231");
        list.add(tGdfwPqgd);
    }

    @Override
    public void getPqgdSuccess(MPageInfo data) {
          commonTabLayout.showMsg(1, data.getTotal());
          list.addAll(data.getList());
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

//    protected int dp2px(float dp) {
//        final float scale = mContext.getResources().getDisplayMetrics().density;
//        return (int) (dp * scale + 0.5f);
//    }

    @Override
    public void onBackPressed() {
        long currentTimeMillis=System.currentTimeMillis();
        if (currentTimeMillis-lastTimeMillis>2000){
            showToast("再次点击返回桌面");
            lastTimeMillis=currentTimeMillis;
        }else {
            //直接返回桌面
            Intent home=new Intent(Intent.ACTION_MAIN);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
        }

    }

    public List<TGdfwPqgd> getList(){
        return list;
    }
}